﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Dapper;
using Infrastructure.Data;
using Microsoft.Extensions.Configuration;
using Repository.DbEntity;
using Repository.IRepository;
using Repository.Model;

namespace Repository.Repository
{
    public class ScoreRepository : AppBaseService<Score>, IScoreRepository
    {
        public ScoreRepository(IConfiguration config)
        {
            Config = config;
        }

        /// <summary>
        /// 成绩管理列表（分页）
        /// </summary>
        /// <param name="studentName">学生姓名</param>
        /// <param name="pageIndex">开始页数</param>
        /// <param name="pageSize">每页条数</param>
        public async Task<PageData<ScoreModel>> GetPageDataAsync(string studentName, int pageIndex, int pageSize)
        {
            var sql = $@"
                    SELECT  ti.* ,
                            m.Name AS MajorName,
                            s.SerialNumber AS StudentNo ,
                            s.RealName AS StudentName,
		                    cou.SerialNumber AS CourseNo ,
                            cou.Name AS CourseName
                    FROM    dbo.T_Score ti
					LEFT JOIN dbo.T_TeachInfo t ON ti.TeachInfoId = t.Id
					LEFT JOIN dbo.T_Student s ON ti.StudentId = s.Id
                            LEFT JOIN dbo.T_Major m ON m.Id = t.MajorId                           
                            LEFT JOIN dbo.T_Course cou ON t.CourseId = cou.Id 
                    WHERE 1=1";
            if (!string.IsNullOrEmpty(studentName))
            {
                sql += $@" s.RealName LIKE @studentName ";
            }

            return await this.PagedAsync<ScoreModel>(sql,
                new { studentName = "%" + studentName + "%" },
                null,
                pageIndex, pageSize);
        }

    }
}
